package org.seamon.management.controller;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.seamon.management.pojo.dto.RoleInsertDto;
import org.seamon.management.pojo.dto.RoleMenuDeleteDto;
import org.seamon.management.pojo.dto.RoleMenuUpdateDto;
import org.seamon.management.pojo.dto.RoleUpdateDto;
import org.seamon.management.service.RoleMenuService;
import org.seamon.management.service.RoleService;
import org.seamon.management.util.Result;
import org.seamon.management.util.aspect.Limit;
import org.seamon.management.util.aspect.Log;
import org.seamon.management.util.enums.BusinessTypeEnum;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author Simon
 */
@RestController
@RequestMapping("/api/role")
public class RoleController {
    @Resource
    private RoleService roleService;
    @Resource
    private RoleMenuService roleMenuService;

    @PreAuthorize("hasAuthority('role:list')")
    @Log(title = "角色列表", businessType = BusinessTypeEnum.LIST)
    @GetMapping("/list")
    public Result<?> list() {
        return Result.success(roleService.listRole());
    }

    @Limit(key = "role_save", permitsPerSecond = 10, timeout = 1, timeunit = TimeUnit.SECONDS)
    @PreAuthorize("hasAuthority('role:add')")
    @Log(title = "角色保存", businessType = BusinessTypeEnum.ADD)
    @PostMapping()
    public Result<?> save(@RequestBody @Valid RoleInsertDto dto) {
        roleService.save(dto);
        return Result.success();
    }

    @Limit(key = "role_update", permitsPerSecond = 10, timeout = 1, timeunit = TimeUnit.SECONDS)
    @PreAuthorize("hasAuthority('role:edit')")
    @Log(title = "角色更新", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping()
    public Result<?> update(@RequestBody @Valid RoleUpdateDto dto) {
        roleService.update(dto);
        return Result.success();
    }

    @Limit(key = "role_delete", permitsPerSecond = 10, timeout = 1, timeunit = TimeUnit.SECONDS)
    @PreAuthorize("hasAuthority('role:delete')")
    @Log(title = "角色删除", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable @NotNull Long id) {
        roleService.removeRoleById(id);
        return Result.success();
    }

    @Limit(key = "role_updateMenu", permitsPerSecond = 10, timeout = 1, timeunit = TimeUnit.SECONDS)
    @PreAuthorize("hasAuthority('role:assignMenu')")
    @Log(title = "角色菜单保存", businessType = BusinessTypeEnum.UPDATE)
    @PostMapping("/menu")
    public Result<?> updateMenu(@RequestBody @Valid RoleMenuUpdateDto dto) {
        roleMenuService.updateMenu(dto);
        return Result.success();
    }

    @Limit(key = "role_deleteMenu", permitsPerSecond = 10, timeout = 1, timeunit = TimeUnit.SECONDS)
    @PreAuthorize("hasAuthority('role:deleteMenu')")
    @Log(title = "角色菜单删除", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/menu")
    public Result<?> deleteMenu(@RequestParam @Valid RoleMenuDeleteDto dto) {
        roleMenuService.remove(dto);
        return Result.success();
    }
}
